package com.chen.controller;


import com.chen.context.BaseContext;
import com.chen.pojo.dto.ContactPersonDto;
import com.chen.pojo.vo.ContactPersonVo;
import com.chen.results.Result;
import com.chen.service.ContactPersonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 陈子安
 * @since 2023-11-30
 */
@RestController
@RequestMapping("/contact-person")
@Slf4j
@Api(tags = "联系人接口")
public class ContactPersonController {
    @Autowired
    private ContactPersonService contactPersonService;


    @GetMapping("contactsAll")
    @ApiOperation("获取所有联系人的信息")
    public Result<List<ContactPersonVo>> getContactsAll() {
        log.info("当前的用户为：{}", BaseContext.getCurrentId());
        List<ContactPersonVo> contactsAll = contactPersonService.getContactsAll();
        return Result.success(contactsAll);
    }

    @GetMapping("{contactId}")
    @ApiOperation("根据联系人id查看联系人信息")
    public Result<ContactPersonVo> getContactByContactId(@PathVariable("contactId") Integer contactId) {
        log.info("要获取的联系人的id为：{}", contactId);
        ContactPersonVo vo = contactPersonService.getContactByContactId2(contactId);
        return Result.success(vo);
    }


    @PostMapping
    @ApiOperation("添加联系人")
    public Result<ContactPersonVo> addContactPerson(@RequestBody ContactPersonDto contactPersonDto) {
        log.info("要添加的用户的信息为：{}", contactPersonDto);
        ContactPersonVo contactPersonVo = contactPersonService.addContactPerson(contactPersonDto);
        return Result.success(contactPersonVo);
    }


    @DeleteMapping("id")
    @ApiOperation("根据id删除联系人")
    public Result deleteContactPersonById(@RequestParam("id") Integer id) {
        log.info("要删除的联系人的id为：{}", id);
        contactPersonService.deleteById(id);
        return Result.success();
    }


}
